package com.blockmeta.bbs.businesslibrary.widget;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blockmeta.bbs.baselibrary.utils.LoadPicUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.net.pojo.GetChatGroupByUserIdResponse;
import com.blockmeta.bbs.businesslibrary.net.retrofit.RetrofitFactory;
import com.blockmeta.bbs.businesslibrary.net.retrofit.api.NetApi;
import com.blockmeta.bbs.businesslibrary.net.rxjava.CommonSubscriber;
import com.blockmeta.bbs.businesslibrary.net.rxjava.RxUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.List;

public class UserCircleView extends ConstraintLayout {
    private Context mContext;
    private ConstraintLayout clayoutAuthorGroup;
    private LinearLayout layoutAuthorGroupTop;
    private CircleImageView ivAvatar;
    private ImageView ivImSign;
    private TextView tvAuthorGroupname;
    private RoundCornerTextView tvCircleIsmy;
    private TextView tvAuthorGroupdesc;
    private LinearLayout layoutAuthorGroupBottom;


    public UserCircleView(Context context) {
        super(context);
        init(context);
    }

    public UserCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UserCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        mContext = context;
        inflate(context, R.layout.layout_mycircle, this);
        clayoutAuthorGroup = (ConstraintLayout) findViewById(R.id.clayout_author_group);
        layoutAuthorGroupTop = (LinearLayout) findViewById(R.id.layout_author_group_top);
        ivAvatar = (CircleImageView) findViewById(R.id.iv_avatar);
        ivImSign = (ImageView) findViewById(R.id.iv_im_sign);
        tvAuthorGroupname = (TextView) findViewById(R.id.tv_author_groupname);
        tvCircleIsmy = (RoundCornerTextView) findViewById(R.id.tv_circle_ismy);
        tvAuthorGroupdesc = (TextView) findViewById(R.id.tv_author_groupdesc);
        layoutAuthorGroupBottom = (LinearLayout) findViewById(R.id.layout_author_group_bottom);


    }

    public void getUserCircleData(int userId) {
        if (userId == 0) {
            return;
        }
        //获取作者创建的群
        RetrofitFactory.getRetrofit().create(NetApi.ListChatGroupByCreateUserId.class)
                .listChatGroupByCreateUserId(userId)
                .compose(RxUtil.<GetChatGroupByUserIdResponse>rxSchedulerHelper())
                .compose(((RxAppCompatActivity) mContext).<GetChatGroupByUserIdResponse>bindToLifecycle())
                .subscribe(new CommonSubscriber<GetChatGroupByUserIdResponse>(mContext) {

                    @Override
                    public void onNext(GetChatGroupByUserIdResponse response) {
                        if (response.code == 200) {
                            List<GetChatGroupByUserIdResponse.ListBean> list = response.list;
                            if (list != null && list.size() > 0) {
                                clayoutAuthorGroup.setVisibility(VISIBLE);
                                final GetChatGroupByUserIdResponse.ListBean listBean = list.get(0);
                                LoadPicUtil.loadWithPicasso(ivAvatar, listBean.getGroupLogo());
                                tvAuthorGroupname.setText(listBean.getChatGroupName());
                                tvAuthorGroupdesc.setText(listBean.getDescription());
                                ivImSign.setVisibility(listBean.getIdentification() > 0 ? VISIBLE : GONE);
                                clayoutAuthorGroup.setOnClickListener(new OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        RouterHelper.startActivity(mContext, RouterPath.IM_CircleDetailActivity_ACTIVITY,  listBean.getId()+ "");
                                    }
                                });
                            }
                        }
                    }
                });
    }

}

